package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.vo.AdopterAccountUserVO;
import com.tbit.uqbike.object.pojo.vo.AdopterMachineVO;
import com.tbit.uqbike.object.pojo.vo.AdopterOperatingStat;
import com.tbit.uqbike.object.pojo.vo.AdopterOperatingStatVO;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.AdopterMsgService;
import com.tbit.uqbike.webmanager.dao.capital.RideLogDetailsDao;
import com.tbit.uqbike.webmanager.dao.core.AdoptMachineBoundDao;
import com.tbit.uqbike.webmanager.dao.core.AdopterMsgDao;
import com.tbit.utils.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author:BUGTian
 * @DATE: 2022/9/27
 */
@Service
public class AdopterMsgServiceImpl implements AdopterMsgService {
    @Autowired
    private AdopterMsgDao adopterMsgDao;
    @Autowired
    private AdoptMachineBoundDao adoptMachineBoundDao;
    @Autowired
    private RideLogDetailsDao rideLogDetailsDao;

    /**
     * 获取两个时间之间的每一天
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<String> getBetweenEveryDay(Date startTime, Date endTime) {
        if (startTime == null || endTime == null || (startTime.getTime() > endTime.getTime())) {
            //防止死循环
            throw new BaseException("时间有误");
        }
        List<String> dateList = new LinkedList<>();
        long tempTime = startTime.getTime();
        long endTimeStamp = endTime.getTime();
        //一天的时间
        long oneDayStamp = 1000 * 60 * 60 * 24;
        while (tempTime <= endTimeStamp) {
            dateList.add(DateUtil.formatDate(new Date(tempTime)));
            tempTime += oneDayStamp;
        }
        return dateList;
    }

    public static void main(String[] args) {
        DateTime yesterday = DateUtil.yesterday();
        DateTime tomorrow = DateUtil.tomorrow();
        System.out.println(yesterday + " - " + tomorrow);
        List<String> betweenEveryDay = getBetweenEveryDay(yesterday, tomorrow);
        System.out.println(betweenEveryDay);
    }

    @Override
    public AdopterOperatingStatVO adopterOperatingStat(Integer accountUserId, Integer adAccountId, Date startTime, Date endTime, Integer adopterAccountUserId) {
        //如果传了领养人账号id过来，则是有下拉框权限的，使用该id进行查询
        if (adopterAccountUserId != null) {
            accountUserId = adopterAccountUserId;
        }
        List<Integer> machineIds=adoptMachineBoundDao.selectByMachineId(accountUserId);
        List<AdopterOperatingStat> adopterOperatingStatList=new ArrayList<>();
        if(!machineIds.isEmpty()){
            adopterOperatingStatList = rideLogDetailsDao.selectAdopterOperatingStat(accountUserId, adAccountId, startTime, endTime,machineIds.size(),machineIds);
        }
        List<String> betweenEveryDay = getBetweenEveryDay(startTime, endTime);
        //总金额
        double totalMoney = 0;
        //总订单数
        int totalOrderNumber = 0;
        Map<String, AdopterOperatingStat> map = new HashMap<>();
        List<AdopterOperatingStat> list = new ArrayList<>();
        int machineNumber = 0;
        for (AdopterOperatingStat adopterOperatingStat : adopterOperatingStatList) {
            if (adopterOperatingStat.getDateTime() == null) {
                //为空的是未支付的，不计算入内
                continue;
            }
            machineNumber = adopterOperatingStat.getMachineCount();
            map.put(adopterOperatingStat.getDateTime(), adopterOperatingStat);
            totalMoney += adopterOperatingStat.getMoney();
            totalOrderNumber += adopterOperatingStat.getOrderNumber();
        }
        for (String s : betweenEveryDay) {
            AdopterOperatingStat e = map.get(s);
            list.add(e == null ? new AdopterOperatingStat(s, 0D, 0, machineNumber) : e);
        }
        return new AdopterOperatingStatVO(totalMoney, totalOrderNumber, list);
    }

    @Override
    public Result<PageResult<AdopterMachineVO>> queryAdopterMachine(Integer page, Integer size, Integer accountUserId, Integer adAccountId, String machineNO, Integer state, Date startTime, Date endTime) {
        PageInfo<AdopterMachineVO> pageInfo = PageHelper.startPage(page, size).doSelectPageInfo(() -> adopterMsgDao.queryAdopterMachine(accountUserId, adAccountId, machineNO, state, startTime, endTime));
        return Result.success(new PageResult<>(page, size, pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getList()));
    }

    @Override
    public List<AdopterAccountUserVO> queryAdopterAccountUser(Integer accountId) {
        return adopterMsgDao.queryAdopterAccountUser(accountId);
    }

}


